/**
 * Created by Ren on 2015/6/3.
 */
function Enemy(ctx, enemyAudio, x, y, player, direction, enemyBullet, enemy, bulletAudio, type)
{
    this.ctx = ctx;
    this.bulletAudio = bulletAudio;
    this.img = new Image();
    this.img.src = "images/Rock.png";
    this.x = x;
    this.y = y;
    this. r = 0;
    this.speed = Math.random() + 1;
    this.bulSpeed = 3;
    this.exploded = false;
    this.explodedImg = new Image();
    this.explodedImg.src = "images/explosionEnemy.png";
    this.explodedIndex = 0;
    this.enemyAudio = enemyAudio;
    this.enemy = enemy;
    this.enemyBullet = enemyBullet;
    this.fireTime = 0;
    this.direction = direction;
    this.player = player;
    this.width = 66;
    this.height = 70;
    this.enemyLife = 0;
    this.isShow = false;
    this.type = type;
    this.dx = Math.random()*33 + 20;
    this.dy = Math.random()*35 + 20;
}
Enemy.prototype.draw = function() {
    if(gameStatus.game_Status != 0 && gameStatus.game_Status != 2)
    {
        if(!this.exploded)
        {
            if(this.isShow)
            {
                this.explosion();
            }
            this.fireTime++;
            this.r = this.r + 2 / 180 * Math.PI;
            this.r = this.r % (2 * Math.PI);
            drawImage2(this.ctx, this.img, 0, 0, 66, 70, this.x, this.y, this.r, this.dx, this.dy);
            switch (this.direction)
            {
                case 0:
                    this.y+=this.speed;
                    break;
                case 1:
                    this.y+=this.speed;
                    this.x+=this.speed;
                    break;
                case 2:
                    this.y+=this.speed;
                    this.x-=this.speed;
                    break;
            }
            if(this.fireTime > gameStatus.game_bossFireSpeed)
            {
                this.fire();
                this.fireTime = 0;
            }
        }
        else
        {
            this.explosion();
            this.enemy.remove(this);
        }
    }
    else
    {
        this.enemy.removeALL();
        this.enemyBullet.removeALL();
    }
}
Enemy.prototype.fire = function()
{
    var direction;
    if(this.player.x < this.x)
    {
        if(this.player.y < this.y)
        {
            direction = 5;
        }
        else
        {
            direction = 3;
        }
    }
    else
    {
        if(this.player.y < this.y)
        {
            direction = 4;
        }
        else
        {
            direction = 2;
        }
    }
    var bullet = new Bullet(this.ctx, this.x + this.dx/2, this.y + this.dy/2, this.enemyBullet, this.bulSpeed, direction, "images/bullet.png", this.bulletAudio, 0, 16, 28);
    this.enemyBullet.push(bullet);
}
Enemy.prototype.getCenter=function() {
    return new Point(this.x + this.dx/2, this.y + this.dy/2);
}
Enemy.prototype.explosion=function() {
    if(this.enemyAudio && this.exploded)
    {
        if(this.explodedIndex < 10)
        {
            this.enemyAudio.currentTime = 0;
            this.enemyAudio.pause();
            this.ctx.drawImage(this.explodedImg,this.explodedIndex*44, 0, 44, 49, this.x,this.y, 44, 49);
            this.explodedIndex ++;
            this.enemyAudio.play();
        }
    }
    else
    {
        if(this.explodedIndex < 4)
        {
            this.ctx.drawImage(this.explodedImg,this.explodedIndex*44, 0, 44, 49, this.x,this.y, 44, 49);
            this.explodedIndex ++;
        }
        this.isShow = false;
        this.explodedIndex = 0;
    }
}